﻿using System;
using System.Collections.Generic;
using System.Data;
using RpcHelper;
using SqlExecHelper;
using SqlExecHelper.Column;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Helper
{
    internal static class WhereLinq
    {
        public static ISqlWhere[] ToWhere(this TransmitQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(9);
            if (query.ServerId.HasValue)
            {
                list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = query.ServerId.Value });
            }
            else
            {
                if (query.SystemTypeId.HasValue)
                {
                    list.Add(new SqlWhere("SystemTypeId", SqlDbType.BigInt) { Value = query.SystemTypeId.Value });
                }
                if (query.RpcMerId.HasValue)
                {
                    list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId.Value });
                }
            }
            if (!query.Scheme.IsNull())
            {
                list.Add(new SqlWhere("Scheme", SqlDbType.VarChar, 50) { Value = query.Scheme });
            }
            if (query.TransmitType.HasValue)
            {
                list.Add(new SqlWhere("TransmitType", SqlDbType.SmallInt) { Value = query.TransmitType.Value });
            }
            if (query.IsEnable.HasValue)
            {
                list.Add(new SqlWhere("IsEnable", SqlDbType.Bit) { Value = query.IsEnable.Value });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this ShieIdQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(5);
            if (query.RpcMerId.HasValue)
            {
                list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId.Value });
            }
            if (!query.SystemType.IsNull())
            {
                list.Add(new SqlWhere("SystemType", SqlDbType.VarChar, 50) { Value = query.SystemType });
            }

            if (query.ServerId.HasValue)
            {
                list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = query.ServerId.Value });
            }
            if (!query.Path.IsNull())
            {
                list.Add(new LikeSqlWhere("ResourcePath", 100, LikeQueryType.右) { Value = query.Path });
            }
            if (query.IsOverTime.HasValue)
            {
                long now = DateTime.Now.ToLong();
                if (query.IsOverTime.Value)
                {
                    list.Add(new SqlWhere("BeOverdueTime", SqlDbType.BigInt, QueryType.小等) { Value = now });
                }
                else
                {
                    list.Add(new SqlWhere("BeOverdueTime", SqlDbType.BigInt, QueryType.大于) { Value = now });
                }
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this IpBlackQuery query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(9);
            if (query.RpcMerId.HasValue)
            {
                list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId.Value });
            }
            if (!query.SystemType.IsNull())
            {
                list.Add(new SqlWhere("SystemType", SqlDbType.VarChar, 50) { Value = query.SystemType });
            }
            if (query.Ip.HasValue)
            {
                list.Add(new SqlAndOrColumn(new ISqlWhere[] {
                    new SqlWhere("Ip", SqlDbType.BigInt) { Value = query.Ip.Value },
                    new BetweenSqlWhere<long>(SqlDbType.BigInt,query.Ip.Value,"Ip","EndIp")
                })
                {
                    IsAnd = false
                });
            }
            if (query.IsDrop.HasValue)
            {
                list.Add(new SqlWhere("IsDrop", SqlDbType.Bit) { Value = query.IsDrop.Value });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this TraceQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(9);
            if (query.ServerId.HasValue)
            {
                list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = query.ServerId.Value });
            }
            if (query.SystemTypeId.HasValue)
            {
                list.Add(new SqlWhere("SystemTypeId", SqlDbType.BigInt) { Value = query.SystemTypeId.Value });
            }
            if (query.RegionId.HasValue)
            {
                list.Add(new SqlWhere("RegionId", SqlDbType.BigInt) { Value = query.RegionId.Value });
            }
            if (!query.TraceId.IsNull())
            {
                list.Add(new SqlWhere("TraceId", SqlDbType.VarChar, 32) { Value = query.TraceId });
            }
            if (query.Begin.HasValue && query.End.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("BeginTime", SqlDbType.DateTime, query.Begin.Value, query.End.Value));
            }
            else if (query.Begin.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("BeginTime", SqlDbType.DateTime, query.Begin.Value, DateTime.Now));
            }
            else if (query.End.HasValue)
            {
                list.Add(new SqlWhere("BeginTime", SqlDbType.DateTime, QueryType.小等) { Value = query.End });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this RootQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(9);
            if (query.RpcMerId.HasValue)
            {
                list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId.Value });
            }

            if (query.ServerId.HasValue)
            {
                list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = query.ServerId.Value });
            }
            if (query.SystemTypeId.HasValue)
            {
                list.Add(new SqlWhere("SystemTypeId", SqlDbType.BigInt) { Value = query.SystemTypeId.Value });
            }
            if (query.RegionId.HasValue)
            {
                list.Add(new SqlWhere("RegionId", SqlDbType.BigInt) { Value = query.RegionId.Value });
            }
            if (query.TranStatus.HasValue)
            {
                list.Add(new SqlWhere("TranStatus", SqlDbType.SmallInt) { Value = query.TranStatus.Value });
            }
            list.Add(new SqlWhere("IsRoot", SqlDbType.Bit) { Value = true });
            if (query.Begin.HasValue && query.End.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("AddTime", SqlDbType.DateTime, query.Begin.Value, query.End.Value));
            }
            else if (query.Begin.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("AddTime", SqlDbType.DateTime, query.Begin.Value, DateTime.Now));
            }
            else if (query.End.HasValue)
            {
                list.Add(new SqlWhere("AddTime", SqlDbType.DateTime, QueryType.小等) { Value = query.End });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this BroadcastQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(5);
            if (query.RpcMerId.HasValue)
            {
                list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId.Value });
            }

            if (query.ServerId.HasValue)
            {
                list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = query.ServerId.Value });
            }
            if (query.ErrorCode.HasValue)
            {
                list.Add(new SqlWhere("ErrorCode", SqlDbType.BigInt) { Value = query.ErrorCode.Value });
            }
            if (!query.MsgKey.IsNull())
            {
                list.Add(new SqlWhere("MsgKey", SqlDbType.VarChar, 50) { Value = query.MsgKey });
            }
            if (query.Begin.HasValue && query.End.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("AddTime", SqlDbType.DateTime, query.Begin.Value, query.End.Value));
            }
            else if (query.Begin.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("AddTime", SqlDbType.DateTime, query.Begin.Value, DateTime.Now));
            }
            else if (query.End.HasValue)
            {
                list.Add(new SqlWhere("AddTime", SqlDbType.DateTime, QueryType.小等) { Value = query.End });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this LogQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(7);
            if (query.RpcMerId.HasValue)
            {
                list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId.Value });
            }
            if (!query.TraceId.IsNull())
            {
                list.Add(new SqlWhere("TraceId", SqlDbType.VarChar, 64) { Value = query.TraceId });
            }
            if (query.ServerId.HasValue)
            {
                list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = query.ServerId.Value });
            }
            else if (query.SystemType.IsNotNull())
            {
                list.Add(new SqlWhere("SystemType", SqlDbType.VarChar, 50) { Value = query.SystemType });
            }
            if (query.LogType.HasValue)
            {
                list.Add(new SqlWhere("LogType", SqlDbType.TinyInt) { Value = query.LogType.Value });
            }
            if (query.LogGrade.HasValue)
            {
                list.Add(new SqlWhere("LogGrade", SqlDbType.TinyInt) { Value = query.LogGrade.Value });
            }
            if (!query.LogGroup.IsNull())
            {
                list.Add(new SqlWhere("LogGroup", SqlDbType.VarChar, 50) { Value = query.LogGroup });
            }
            if (query.Begin.HasValue && query.End.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("AddTime", SqlDbType.DateTime, query.Begin.Value, query.End.Value));
            }
            else if (query.Begin.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("AddTime", SqlDbType.DateTime, query.Begin.Value, DateTime.Now));
            }
            else if (query.End.HasValue)
            {
                list.Add(new SqlWhere("AddTime", SqlDbType.DateTime, QueryType.小等) { Value = query.End });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this TaskLogQuery query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(5)
                        {
                                new SqlWhere ("TaskId", SqlDbType.UniqueIdentifier) { Value = query.TaskId }
                        };
            if (query.ItemId.HasValue)
            {
                list.Add(new SqlWhere("ItemId", SqlDbType.UniqueIdentifier) { Value = query.ItemId.Value });
            }
            if (query.IsFail.HasValue)
            {
                list.Add(new SqlWhere("IsFail", SqlDbType.Bit) { Value = query.IsFail.Value });
            }
            if (query.Begin.HasValue && query.End.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("BeginTime", SqlDbType.DateTime, query.Begin.Value, query.End.Value));
            }
            else if (query.Begin.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("BeginTime", SqlDbType.DateTime, query.Begin.Value, DateTime.Now));
            }
            else if (query.End.HasValue)
            {
                list.Add(new SqlWhere("BeginTime", SqlDbType.DateTime, QueryType.小等) { Value = query.End });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this ResourceQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(4)
                        {
                                new SqlWhere ("ModularId", SqlDbType.UniqueIdentifier) { Value = query.ModularId }
                        };
            if (query.ResourceState.HasValue)
            {
                list.Add(new SqlWhere("ResourceState", SqlDbType.SmallInt) { Value = query.ResourceState.Value });
            }
            if (query.VerNum.HasValue)
            {
                list.Add(new SqlWhere("VerNum", SqlDbType.BigInt) { Value = query.VerNum.Value });
            }
            if (query.ResourceVer.HasValue)
            {
                list.Add(new SqlWhere("ResourceVer", SqlDbType.Int) { Value = query.ResourceVer.Value });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this ModularQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(5)
                        {
                                new SqlWhere ("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId }
                        };
            if (query.SysGroupId.HasValue)
            {
                list.Add(new SqlWhere("SysGroupId", SqlDbType.BigInt) { Value = query.SysGroupId.Value });
            }
            if (query.ResourceType.HasValue)
            {
                list.Add(new SqlWhere("ResourceType", SqlDbType.SmallInt) { Value = query.ResourceType.Value });
            }
            if (!query.ModularName.IsNull())
            {
                list.Add(new LikeSqlWhere("ModularName", 50, LikeQueryType.全) { Value = query.ModularName });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this IdentityQueryParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(5);
            if (query.Begin.HasValue && query.End.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("EffectiveDate", SqlDbType.Date, query.Begin.Value, query.End.Value));
            }
            else if (query.Begin.HasValue)
            {
                list.Add(new BetweenSqlWhere<DateTime>("EffectiveDate", SqlDbType.Date, query.Begin.Value, HeartbeatTimeHelper.CurrentDate));
            }
            else if (query.End.HasValue)
            {
                list.Add(new SqlWhere("EffectiveDate", SqlDbType.Date, QueryType.小等) { Value = query.End });
            }
            if (query.IsEnable.HasValue)
            {
                list.Add(new SqlWhere("IsEnable", SqlDbType.Bit) { Value = query.IsEnable.Value });
            }
            if (!query.AppName.IsNull())
            {
                list.Add(new LikeSqlWhere("AppName", 50, LikeQueryType.全) { Value = query.AppName });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this QuerySysParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(3);
            if (query.Range.HasValue)
            {
                if (query.Range.Value == ConfigRange.全局)
                {
                    if (query.RpcMerId.HasValue)
                    {
                        list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId });
                    }
                    else
                    {
                        list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = 0 });
                    }
                    list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = 0 });
                    list.Add(new SqlWhere("SystemType", SqlDbType.VarChar, 50) { Value = string.Empty });
                }
                else if (query.Range.Value == ConfigRange.节点)
                {
                    list.Add(new SqlWhere("ServerId", SqlDbType.BigInt, QueryType.不等) { Value = 0 });
                }
                else
                {
                    if ((ConfigRange.集群 & query.Range.Value) == ConfigRange.集群)
                    {
                        list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt, QueryType.不等) { Value = 0 });
                    }
                    if ((ConfigRange.节点类别 & query.Range.Value) == ConfigRange.节点类别)
                    {
                        if (query.RpcMerId.HasValue)
                        {
                            list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId.Value });
                        }
                        list.Add(new SqlWhere("SystemType", SqlDbType.BigInt, QueryType.不等) { Value = string.Empty });
                    }
                    list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = 0 });
                }
            }
            else
            {
                if (query.ServerId.HasValue)
                {
                    list.Add(new SqlWhere("ServerId", SqlDbType.BigInt) { Value = query.ServerId.Value });
                }
                else if (query.RpcMerId.HasValue)
                {
                    list.Add(new SqlWhere("RpcMerId", SqlDbType.BigInt) { Value = query.RpcMerId.Value });
                }
                if (!query.SystemType.IsNull())
                {
                    list.Add(new SqlWhere("SystemType", SqlDbType.VarChar, 50) { Value = query.SystemType });
                }
            }
            if (!string.IsNullOrEmpty(query.ConfigName))
            {
                list.Add(new SqlWhere("Name", SqlDbType.VarChar, 50) { Value = query.ConfigName });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this QueryServiceParam query)
        {
            List<ISqlWhere> list = new List<ISqlWhere>(5);
            if (query.SystemTypeId.HasValue)
            {
                list.Add(new SqlWhere("SystemType", SqlDbType.BigInt) { Value = query.SystemTypeId.Value });
            }
            else if (query.GroupId.HasValue)
            {
                list.Add(new SqlWhere("GroupId", SqlDbType.BigInt) { Value = query.GroupId.Value });
            }
            if (query.IsOnline.HasValue)
            {
                list.Add(new SqlWhere("IsOnline", SqlDbType.Bit) { Value = query.IsOnline });
            }
            if (query.ServiceState.HasValue)
            {
                list.Add(new SqlWhere("ServiceState", SqlDbType.SmallInt) { Value = query.ServiceState.Value });
            }
            if (query.RegionId.HasValue)
            {
                list.Add(new SqlWhere("RegionId", SqlDbType.BigInt) { Value = query.RegionId.Value });
            }
            if (query.IsContainer.HasValue)
            {
                list.Add(new SqlWhere("IsContainer", SqlDbType.Bit) { Value = query.IsContainer.Value });
            }
            if (!query.ServiceMac.IsNull())
            {
                list.Add(new SqlWhere("ServiceMac", SqlDbType.VarChar, 17) { Value = query.ServiceMac });
            }
            else if (!query.ServiceName.IsNull())
            {
                list.Add(new LikeSqlWhere("ServerName", 50, LikeQueryType.全) { Value = query.ServiceName });
            }
            return list.ToArray();
        }
        public static ISqlWhere[] ToWhere(this VerQueryParam query)
        {
            List<ISqlWhere> wheres = new List<ISqlWhere>(4);
            if (query.Ver.HasValue)
            {
                wheres.Add(new SqlWhere("VerNum", System.Data.SqlDbType.BigInt) { Value = query.Ver.Value });
            }
            if (query.SystemTypeId.HasValue)
            {
                wheres.Add(new SqlWhere("SystemTypeId", System.Data.SqlDbType.BigInt) { Value = query.SystemTypeId.Value });
            }
            if (query.IsEnable.HasValue)
            {
                wheres.Add(new SqlWhere("IsEnable", System.Data.SqlDbType.Bit) { Value = query.IsEnable.Value });
            }
            if (!query.VerTitle.IsNull())
            {
                wheres.Add(new LikeSqlWhere("VerTitle", 50) { Value = query.VerTitle });
            }
            return wheres.ToArray();
        }
        public static ISqlWhere[] ToWhere(this QueryTaskParam query)
        {
            List<ISqlWhere> wheres = new List<ISqlWhere>(4);
            if (query.RegionId.HasValue)
            {
                wheres.Add(new SqlWhere("RegionId", System.Data.SqlDbType.BigInt) { Value = query.RegionId.Value });
            }
            if (query.IsExec.HasValue)
            {
                wheres.Add(new SqlWhere("IsExec", System.Data.SqlDbType.Bit) { Value = query.IsExec.Value });
            }
            if (query.IsEnable.HasValue)
            {
                wheres.Add(new SqlWhere("IsEnable", System.Data.SqlDbType.Bit) { Value = query.IsEnable.Value });
            }
            if (!query.TaskName.IsNull())
            {
                wheres.Add(new LikeSqlWhere("TaskName", 50) { Value = query.TaskName });
            }
            return wheres.ToArray();
        }
    }
}
